package cn.tedu.boot33.controller;

import cn.tedu.boot33.entity.Hero;
import cn.tedu.boot33.mapper.HeroMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HeroController {
    @Autowired(required = false)
    HeroMapper heroMapper;

    @RequestMapping("/add")
    public String add(Hero hero){
        heroMapper.insert(hero);

        return "添加完成!";
    }

    @RequestMapping("/select")
    public String select(){
        String html = "<center>\n" +
                "    <table border=\"1px\">\n" +
                "        <caption>英雄列表</caption>\n" +
                "        <tr>\n" +
                "            <th>id</th>\n" +
                "            <th>名字</th>\n" +
                "            <th>类型</th>\n" +
                "            <th>价格</th>\n" +
                "        </tr>";
        List<Hero> heroes = heroMapper.select();
        for (Hero hero : heroes) {
            html += "<tr>\n" +
                    "        <td>"+ hero.getId() +"</td>\n" +
                    "        <td>"+ hero.getName() +"</td>\n" +
                    "        <td>"+ hero.getType() +"</td>\n" +
                    "        <td>"+ hero.getMoney() +"</td>\n" +
                    "    </tr>";
        }
        html += " </table>\n" +
                "</center>";
        return html;
    }
}
